#!/bin/sh
#/home/mysqlbak/scripts/mysql_bak.sh
# Create Date:  2018-10-09
# Description:  database backup
# 0 3 * * * /home/mysqlbak/scripts/mysql_bak.sh >/dev/null 2>>/home/mysqlbak/scripts/mysql_bak.log
if [ `id -u` -ne 0 ];then
    echo "非root用户，脚本退出"
    exit 0
fi
MYSQL_PATH="/usr/local/mysql/bin"
curdate=`date +%F-%H-%M-%S`
#备份
if [ ! -x "/home/mysqlbak/db_bak/$curdate" ]; then
   mkdir -p /home/mysqlbak/db_bak/$curdate
fi
DBLIST=`$MYSQL_PATH/mysql -uroot -p'123456' -e "show databases;" 2>/dev/null | sed 1d |egrep -v "_schema|mysql"`
for dbname in $DBLIST
do
   $MYSQL_PATH/mysqldump -uroot -p'123456' -F -R --single-transaction $dbname |gzip > /home/mysqlbak/db_bak/$curdate/$dbname-$curdate.sql.gz
done
##删除60天前的旧备份
find /home/mysqlbak/db_bak -ctime +60 -type f | xargs rm -rf
